package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.PanicBuyingGoods;
import com.rongke.model.PanicBuyingTime;
import com.rongke.service.PanicBuyingGoodsService;
import com.rongke.service.PanicBuyingTimeService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @PanicBuyingTimeController
 * @抢购时间Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/panicBuyingTime")
@Transactional
@CrossOrigin
public class PanicBuyingTimeController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private PanicBuyingTimeService panicBuyingTimeService;
    @Autowired
    private PanicBuyingGoodsService panicBuyingGoodsService;
    /**
     * @添加抢购时间
     * @param panicBuyingTime
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addPanicBuyingTime(@RequestBody PanicBuyingTime panicBuyingTime){
        log.debug("添加抢购时间");
        EntityWrapper<PanicBuyingTime> ewt=new EntityWrapper<>();
        ewt.lt("end_date",new Date());
        List<PanicBuyingTime> pbtList=panicBuyingTimeService.selectList(ewt);
        String pbtIds="";
         if(!pbtList.isEmpty()){
             for (PanicBuyingTime pbt:pbtList) {
                 pbtIds+=pbt.getId()+",";
             }
             if(pbtIds!=null){
                 pbtIds=pbtIds.substring(0,pbtIds.length()-1);
                 //删除已过期的时间段及其商品
                 EntityWrapper<PanicBuyingGoods> ew=new EntityWrapper<>();
                 ew.where("find_in_set(panic_buying_goods.pbt_id,'"+pbtIds+"') >0","panic_buying_goods.pbt_id");
                 panicBuyingGoodsService.delete(ew);
             }
             /*先删除时间段会报外键约束错误*/
             panicBuyingTimeService.delete(ewt);
         }

        //添加新的抢购时间段
        panicBuyingTimeService.insert(panicBuyingTime);
        return JsonResp.ok(panicBuyingTime);
    }

    /**
     * @修改抢购时间
     * @param panicBuyingTime
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updatePanicBuyingTime(@RequestBody PanicBuyingTime panicBuyingTime){
        log.debug("修改抢购时间");
        panicBuyingTimeService.updateById(panicBuyingTime);
        return JsonResp.ok(panicBuyingTime);
    }
    /**
     * @根据id查找抢购时间
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectList", method = RequestMethod.GET)
    public JsonResp selectPanicBuyingTime() {
        log.debug("查找抢购时间");
        EntityWrapper<PanicBuyingTime> ew = new EntityWrapper<>();
        String sdf=new SimpleDateFormat("yyyy-MM-dd").format(new Date())+" 23:59:59";
        Date date=null;
        try {
            date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdf);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ew.le("start_date", date);
//        ew.orderBy("start_date", true);
        List<PanicBuyingTime> panicBuyingTimeList = panicBuyingTimeService.selectList(ew);
         //排序按时间(时、分)排序
        List<PanicBuyingTime> currentBuyingTime=new ArrayList<>();
        int i = 0, y = panicBuyingTimeList.size();
        for (; i < y; i++) {
            PanicBuyingTime pbt = panicBuyingTimeList.get(i);
            for(int j=i;j<y;j++){
                PanicBuyingTime nestpbt = panicBuyingTimeList.get(j);
                String t1=new SimpleDateFormat("HH:mm").format(pbt.getStartDate());
                String t2=new SimpleDateFormat("HH:mm").format(nestpbt.getStartDate());
                if(t1.compareTo(t2)>0){
                    panicBuyingTimeList.set(i,nestpbt);
                    panicBuyingTimeList.set(j,pbt);
                }
            }
        }
        //如果查询日期列表中有今日的日期  显示今日的日期
        for (int m=0;m<y;m++) {
            PanicBuyingTime current=panicBuyingTimeList.get(m);
            String currentDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            if ( currentDate.compareTo(new SimpleDateFormat("yyyy-MM-dd").format(current.getEndDate())) <= 0){
                currentBuyingTime.add(current);
                if(m<y-1){
                    PanicBuyingTime nest=panicBuyingTimeList.get(m+1);
                    if(new SimpleDateFormat("HH:mm").format(new Date()).compareTo(new SimpleDateFormat("HH:mm").format(current.getStartDate()))<0){
                        current.setStatus("未开始");
                    }else if(new SimpleDateFormat("HH:mm").format(new Date()).compareTo(new SimpleDateFormat("HH:mm").format(nest.getStartDate()))>=0){
                        current.setStatus("已结束");
                    }else {
                        current.setStatus("抢购中");
                    }
                }
               if(m==y-1){
                   if(new SimpleDateFormat("HH:mm").format(new Date()).compareTo(new SimpleDateFormat("HH:mm").format(current.getStartDate()))<0){
                       current.setStatus("未开始");
                   }else if(new SimpleDateFormat("HH:mm").format(new Date()).compareTo(new SimpleDateFormat("HH:mm").format(current.getStartDate()))>=0){
                       current.setStatus("抢购中");
                   }
               }
            }else {
                current.setStatus("已结束");
            }
        }
        Map map = new HashMap();
        map.put("currentTime", new Date());
        if(!currentBuyingTime.isEmpty()){
            map.put("panicBuyingTimeList", currentBuyingTime);
        }else {
            map.put("panicBuyingTimeList", panicBuyingTimeList);
        }
        return JsonResp.ok(map);
    }







    /**
     * @根据条件查找抢购时间
     * @param panicBuyingTime
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectPanicBuyingTimeByCondition(@RequestBody PanicBuyingTime panicBuyingTime){
        log.debug("根据条件查找抢购时间");
        if(panicBuyingTime==null)
            return JsonResp.toFail("参数不能为空");
        List<PanicBuyingTime> panicBuyingTimeList=panicBuyingTimeService.selectByCondition(panicBuyingTime);
        return JsonResp.ok(panicBuyingTimeList);
    }




}
